﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ah.Testudin.Engine.Graphics
{
    public class GraphicsDeviceRenderer : ITestudinRenderer
    {
        #region Private Member Variables

        private readonly GraphicsDevice _graphicsDevice;

        private readonly SpriteBatch _spriteBatch;

        #endregion

        #region Constructors

        public GraphicsDeviceRenderer(
            GraphicsDevice graphicsDevice,
            SpriteBatch spriteBatch)
        {
            if (graphicsDevice == null)
            {
                throw new ArgumentNullException("graphicsDevice");
            }

            if (spriteBatch == null)
            {
                throw new ArgumentNullException("spriteBatch");
            }

            _graphicsDevice = graphicsDevice;

            _spriteBatch = spriteBatch;
        }

        #endregion

        #region IRenderer

        public RasterizerState RasterizerState
        {
            get
            {
                return _graphicsDevice.RasterizerState;
            }

            set
            {
                _graphicsDevice.RasterizerState = value;
            }
        }

        public BlendState BlendState
        {
            get
            {
                return _graphicsDevice.BlendState;
            }
            set
            {
                _graphicsDevice.BlendState = value;
            }
        }

        public DepthStencilState DepthStencilState
        {
            get
            {
                return _graphicsDevice.DepthStencilState;
            }
            set
            {
                _graphicsDevice.DepthStencilState = value;
            }
        }

        public void Clear(Color color)
        {
            _graphicsDevice.Clear(color);
        }

        public void PreRender()
        {
            Clear(Color.Pink);
        }

        public void BeginSpriteBatch()
        {
            _spriteBatch.Begin();
        }

        public void RenderSprite(
            Rectangle rectangle,
            Texture2D texture)
        {
            _spriteBatch.Draw(
                texture,
                rectangle,
                Color.White);
        }

        public void RenderString(
            SpriteFont spriteFont,
            string message,
            Vector2 position,
            Color color)
        {
            _spriteBatch.DrawString(
                spriteFont,
                message,
                position,
                color);
        }

        public void EndSpriteBatch()
        {
            _spriteBatch.End();
        }

        public void DrawUserIndexedPrimitives<T>(
            PrimitiveType primitiveType,
            T[] vertexData,
            int vertexOffset,
            int vertexCount,
            int[] indexData,
            int indexOffset,
            int primitiveCount) where T : struct, IVertexType
        {
            _graphicsDevice.DrawUserIndexedPrimitives<T>(
                primitiveType,
                vertexData,
                vertexOffset,
                vertexCount,
                indexData,
                indexOffset,
                primitiveCount);
        }

        public void PostRender()
        {
            
        }

        #endregion
    }
}
